package it.polimi.reTraveldream.eao;



import it.polimi.reTravelDream.dto.ExcursionDTO;
import it.polimi.reTravelDream.dto.FlightDTO;
import it.polimi.reTravelDream.dto.HotelDTO;
import it.polimi.reTravelDream.dto.SearchPackageDTO;
import it.polimi.reTravelDream.dto.TravelPackageDTO;
import it.polimi.reTravelDream.eao.TravelPackageActionEAOLocal;
import it.polimi.reTraveldream.entities.Excursion;
import it.polimi.reTraveldream.entities.Flight;
import it.polimi.reTraveldream.entities.Hotel;
import it.polimi.reTraveldream.entities.TravelPackage;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 * Session Bean implementation class TravelPackageEAO
 */
@Stateless
public class TravelPackageActionEAO implements TravelPackageActionEAOLocal{

	@PersistenceContext
	private EntityManager em;
	
    public TravelPackageActionEAO() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public List<TravelPackageDTO> searchPackage( SearchPackageDTO searchPackageDTO) {
		TypedQuery<TravelPackage> query= em.createNamedQuery(TravelPackage.FIND_PACKAGE_BY_KEYWORD, TravelPackage.class);
		query.setParameter("keyword",searchPackageDTO.getKeyWord());
		List<TravelPackage> resultEntities=query.getResultList();
		List<TravelPackageDTO> resultDTO= new ArrayList<TravelPackageDTO>();
		 for (TravelPackage t: resultEntities){
			 resultDTO.add(this.convertToDTO(t));
			 }
			return resultDTO;
		 }
	


	@Override
	public List<TravelPackageDTO> getAllPackages() {
		TypedQuery<TravelPackage> query=em.createNamedQuery(TravelPackage.FIND_ALL_PACKAGES, TravelPackage.class);
		List<TravelPackage> resultEntities=query.getResultList();
		
		List<TravelPackageDTO> resultDTO= new ArrayList<TravelPackageDTO>();
		
		for (TravelPackage t: resultEntities){
			 resultDTO.add(this.convertToDTO(t));
			 }
		return resultDTO;	
		}

	
	
	private TravelPackageDTO convertToDTO(TravelPackage travelPackage){
		
		TravelPackageDTO travelPackageDTO= new TravelPackageDTO();
		
		travelPackageDTO.setDescription(travelPackage.getDescription());
		travelPackageDTO.setKeyWord1(travelPackage.getKeyWord1());
		travelPackageDTO.setKeyWord2(travelPackage.getKeyWord2());
		travelPackageDTO.setKeyWord3(travelPackage.getKeyWord3());
		travelPackageDTO.setPackageName(travelPackage.getName());
		travelPackageDTO.setNumberOfPerson(travelPackage.getNumberOfPerson());
		travelPackageDTO.setId(travelPackage.getId());
		travelPackageDTO.setCity(travelPackage.getCity());
		travelPackageDTO.setArrivo(travelPackage.getArrivo());
		travelPackageDTO.setNumberOfNights(travelPackage.getNumberOfNights());
		
		List<Hotel> hotelRelated=new ArrayList<Hotel>();
		List<Flight> flightRelated=new ArrayList<Flight>();
		List<Excursion> excursionsRelated=new ArrayList<Excursion>();
		
		List<HotelDTO> hotelDTOs=new ArrayList<HotelDTO>();
		List<FlightDTO> flightDTOs=new ArrayList<FlightDTO>();
		List<ExcursionDTO> excursionDTOs=new ArrayList<ExcursionDTO>();
		
		hotelRelated=travelPackage.getAvailableHotels();
		flightRelated=travelPackage.getAvailableFlights();
		excursionsRelated=travelPackage.getAvailableExcursions();
		
		for(Hotel hotel:hotelRelated){
			hotelDTOs.add(this.convertToDTO(hotel));
		}
		for(Flight flight:flightRelated){
			flightDTOs.add(this.convertToDTO(flight));
		}
		for(Excursion excursion:excursionsRelated){
			excursionDTOs.add(this.convertToDTO(excursion));
		}
		travelPackageDTO.setPackageFlights(flightDTOs);
		travelPackageDTO.setPackageHotelDTOs(hotelDTOs);
		travelPackageDTO.setPackageExcursions(excursionDTOs);
	

		
		return travelPackageDTO;
	}
	
	 private HotelDTO convertToDTO(Hotel hotelEntity) {
			HotelDTO hotelDTO = new HotelDTO();
			hotelDTO.setId(hotelEntity.getId());
			hotelDTO.setAddress(hotelEntity.getAddress());
			hotelDTO.setDescription(hotelEntity.getDescription());
			hotelDTO.setName(hotelEntity.getName());
			hotelDTO.setStars(hotelEntity.getStars());
			hotelDTO.setPrice(hotelEntity.getPrice());
			hotelDTO.setCity(hotelEntity.getCity());
			return hotelDTO;
			
		}
	 
	 private FlightDTO convertToDTO(Flight hotelEntity) {
			FlightDTO flightDTO = new FlightDTO();
			flightDTO.setId(hotelEntity.getId());
			flightDTO.setArrivalCity(hotelEntity.getArrivalAirport());
			flightDTO.setDepartureCity(hotelEntity.getDepartureAirport());
			flightDTO.setData(hotelEntity.getDate());
			flightDTO.setPrice(hotelEntity.getPrice());
			return flightDTO;
			
		}
	 
	 private ExcursionDTO convertToDTO(Excursion excursionEntity) {
			ExcursionDTO excursionDTO = new ExcursionDTO();
			excursionDTO.setId(excursionEntity.getId());
			excursionDTO.setName(excursionEntity.getName());
			excursionDTO.setDescription(excursionEntity.getDescription());
			excursionDTO.setPrice(excursionEntity.getPrice());
			return excursionDTO;
			
			
		}

	@Override
	public void removePackage(TravelPackageDTO packageToRemove) {
		TravelPackage tPackage=em.find(TravelPackage.class, packageToRemove.getId());
		em.remove(tPackage);
	}

	@Override
	public void modifyPackage(TravelPackageDTO packageToEdit) {
		List<Flight> flightsToAdd=new ArrayList<Flight>();
		List<Hotel> hotelToAdd=new ArrayList<Hotel>();
		List<Excursion> excursionsToAdd= new ArrayList<Excursion>();
		
		List<HotelDTO> hotelList=new ArrayList<HotelDTO>();
		List<FlightDTO> flightList=new ArrayList<FlightDTO>();
		List<ExcursionDTO> excursionList=new ArrayList<ExcursionDTO>();
		hotelList=packageToEdit.getPackageHotelDTOs();
		flightList=packageToEdit.getPackageFlights();
		excursionList=packageToEdit.getPackageExcursions();
		
		for(HotelDTO hDto:hotelList){
			
			hotelToAdd.add(em.find(Hotel.class, hDto.getId()));
		}
		
		for(FlightDTO fDto:flightList){
			flightsToAdd.add(em.find(Flight.class, fDto.getId()));
		}
		for(ExcursionDTO eDto:excursionList){
			excursionsToAdd.add(em.find(Excursion.class, eDto.getId()));
		}

		TravelPackage  tPackage= new TravelPackage(packageToEdit,flightsToAdd,hotelToAdd,excursionsToAdd);	
		em.merge(tPackage);
	}


	@Override
	public void getAllItem(TravelPackageDTO packageSelected) {
		// TODO Auto-generated method stub
		
	}

}
